#include "gdcstuff.h"
#include <iostream>
#include <string.h>

#define LLABEL 16

void debuf2filePrint(FILE *dFile, char *s);

/*
void FreeGDCBjnInfo(LPBJN_INFO pbjn_info)
{
	std::cout << "ERROR FREE GDC\n";
}
*/
void getGridName(char* s, char* gridname, char* gridFileName);

int getFileInfoGDC(char *s, BJN_INFO *bjn_info, FILE *debugFileOutput)
{
	char debugString[1024];
	if(!bjn_info)
		return 15;

	(bjn_info->n1) = (bjn_info->n2) = (bjn_info->n3) =0;


	FILE *fout;

//	long  ret,i;
	static char label[LLABEL+1];

	// R Reading Head of file

	if( (fout=fopen(s,"rb")) == NULL )
	{ 
		sprintf(debugString, "server: getFileInfoGDC: Error: Can't open for input file %s ",s ); 
		printf(debugString);
		debuf2filePrint(debugFileOutput, debugString);
		return 139; 
	}

	int nCoords, nTetra, nTriangl;
	FILE *fMesh;//, *fCoords, *fTetr, *fIter;
	char meshFileName[1024];
	getGridName(s, MESH, meshFileName);
	fMesh = fopen(meshFileName, "r");
	if(NULL != fMesh)
	{
		fscanf(fMesh, "%d\n", &nCoords);
		fscanf(fMesh, "%d\n", &nTetra);
		fscanf(fMesh, "%d\n", &nTriangl);
		fclose(fMesh);
//debug1		std::cout  << "TEST " << nCoords << " " << nTetra << " " << nTriangl << std::endl;
	}
	else {
		sprintf(debugString, "server: getFileInfoGDC: Error: Can't open mesh file %s ",meshFileName ); 
		printf(debugString);
		debuf2filePrint(debugFileOutput, debugString);
		return 43;
	}
	bjn_info->n1 = nCoords;
	bjn_info->n2 = nTetra;
	bjn_info->n3 = nTriangl;
//	std::cout  << "TEST " << nCoords << " " << nTetra << " " << nTriangl << std::endl;

	bjn_info->n_func = 5;
	FILE *fIter;
//	double *pFunc;
	double d[5];
	char buff[1024];
//	pFunc = new double [nCoords];
//kp97 050810 05:11	fIter = fopen(ITERAT, "r");
	fIter = fopen(s, "r");
	if(NULL != fIter)
	{
		fscanf(fIter, "%s\n", buff);
//debug1		std::cout << " |--"<<buff<<"--| "<<std::endl;
		fscanf(fIter, "%s\n", buff);
//debug1		std::cout << " |--"<<buff<<"--| "<<std::endl;
//		fscanf(fIter, "%s\n", buff);
//		std::cout << " |--"<<buff<<"--| "<<std::endl;
		for(int iCoords = 0 ; iCoords< nCoords; iCoords++)
		{
			fscanf(fIter, "%lf %lf %lf %lf %lf\n", &d[0] /*pFunc[iCoords]*/, &d[1], &d[2], &d[3], &d[4]);
//debug1			std::cout << pFunc[iCoords]<< std::endl;
			if(0 == iCoords) {
				bjn_info->fmin=(LPFLOAT)
					alloc_char_mas_n1(bjn_info->n_func*(sizeof(bjn_info->fmin[0])));

				bjn_info->fmax=(LPFLOAT)
					alloc_char_mas_n1(bjn_info->n_func*(sizeof(bjn_info->fmax[0])));
				for(int j=0; j<5; j++)
				{
					bjn_info->fmin[j] = (float)(d[j]);
					bjn_info->fmax[j] = (float)(d[j]); 
				}
			} else
			{
				for(int jj=0; jj<5; jj++)
				{
					if( bjn_info->fmin[jj] >= (float)d[jj]) bjn_info->fmin[jj] = (float)d[jj];
					if( bjn_info->fmax[jj] <= (float)d[jj]) bjn_info->fmax[jj] = (float)d[jj];
				}
			}
		}
		fclose(fIter);
	}
	else {
		sprintf(debugString, "getFileInfoGDC: Error: Can't open iteration file %s ", s); 
		printf(debugString);
		debuf2filePrint(debugFileOutput, debugString);
		return 44;
	}

	bjn_info->funcNames=(LPLPCHAR)
		alloc_char_mas_n1(bjn_info->n_func*(sizeof(LPCHAR)));

	strncpy(bjn_info->tagOfBjnFile,"GDCponimaesh",LLABEL);
	str_alloc_cpy(bjn_info->filename,s);
	str_alloc_cpy(bjn_info->gridname,COORDS);

	bjn_info->nBitsReduce=(LPINT)
		alloc_char_mas_n1(bjn_info->n_func*(sizeof(bjn_info->nBitsReduce[0])));

	for(int k=0; k<5; k++)
	{
		sprintf(buff, "%d. f%d\0", k+1, k+1);
		str_alloc_cpy(bjn_info->funcNames[k], buff);
		bjn_info->nBitsReduce[k] = 0;
	}

	debuf2filePrint(debugFileOutput, "GDC FileInfo ... done\n");

	return 0;
}



int getGDCgridInfo(char *s, BJN_INFO bjn_info, BJN_INFO *grid_bjninfo, FILE *debugFileOutput)
{
	char debugString[1024];

	if(!grid_bjninfo)
		return 15;

	(grid_bjninfo->n1) = (grid_bjninfo->n2) = (grid_bjninfo->n3) =0;

	grid_bjninfo->n_func = 3;
	int nCoords, nTetra, nTriangl;
	nCoords		=	bjn_info.n1;
	nTetra		=	bjn_info.n2;
	nTriangl	=	bjn_info.n3;
//	std::cout  << "TEST " << nCoords << " " << nTetra << " " << nTriangl << std::endl;

	FILE *fCoords;
	double *pCoords;
	pCoords = new double [nCoords*3];

	char coordsFileName[1024];
	getGridName(s, COORDS, coordsFileName);
	fCoords = fopen(coordsFileName, "r");
	if(NULL != fCoords)
	{
		for(int iCoords = 0 ; iCoords< nCoords; iCoords++) {
			fscanf(fCoords, "%lf %lf %lf\n", &pCoords[iCoords*3], &pCoords[iCoords*3+1], &pCoords[iCoords*3+2]);
			if(0 == iCoords) {
				grid_bjninfo->fmin=(LPFLOAT)
					alloc_char_mas_n1(grid_bjninfo->n_func*(sizeof(grid_bjninfo->fmin[0])));
				grid_bjninfo->fmax=(LPFLOAT)
					alloc_char_mas_n1(grid_bjninfo->n_func*(sizeof(grid_bjninfo->fmax[0])));
				grid_bjninfo->fmin[0] = (float)(pCoords[iCoords*3]);
				grid_bjninfo->fmax[0] = (float)(pCoords[iCoords*3]); 
				grid_bjninfo->fmin[1] = (float)(pCoords[iCoords*3+1]);
				grid_bjninfo->fmax[1] = (float)(pCoords[iCoords*3+1]); 
				grid_bjninfo->fmin[2] = (float)(pCoords[iCoords*3+2]);
				grid_bjninfo->fmax[2] = (float)(pCoords[iCoords*3+2]); 
			} else
			{
				if( grid_bjninfo->fmin[0] > (float)pCoords[iCoords*3]) grid_bjninfo->fmin[0] = (float)pCoords[iCoords*3];
				if( grid_bjninfo->fmax[0] < (float)pCoords[iCoords*3]) grid_bjninfo->fmax[0] = (float)pCoords[iCoords*3];
				if( grid_bjninfo->fmin[1] > (float)pCoords[iCoords*3+1]) grid_bjninfo->fmin[1] = (float)pCoords[iCoords*3+1];
				if( grid_bjninfo->fmax[1] < (float)pCoords[iCoords*3+1]) grid_bjninfo->fmax[1] = (float)pCoords[iCoords*3+1];
				if( grid_bjninfo->fmin[2] > (float)pCoords[iCoords*3+2]) grid_bjninfo->fmin[2] = (float)pCoords[iCoords*3+2];
				if( grid_bjninfo->fmax[2] < (float)pCoords[iCoords*3+2]) grid_bjninfo->fmax[2] = (float)pCoords[iCoords*3+2];
			}
		}
		fclose(fCoords);
//debug1		std::cout << "TEST " << pCoords[nCoords*3-3] << " " << pCoords[nCoords*3-2] << " " << pCoords[nCoords*3-1] << std::endl;
	}
	else {
		sprintf(debugString, "getGDCgridInfo: Error: Can't open coords file %s ", coordsFileName); 
		printf(debugString);
		debuf2filePrint(debugFileOutput, debugString);
		return 45;
	}


	grid_bjninfo->funcNames=(LPLPCHAR)
		alloc_char_mas_n1(grid_bjninfo->n_func*(sizeof(LPCHAR)));

//	str_alloc_cpy(grid_bjninfo->tagOfBjnFile,"GDCponimaesh");
	strncpy(grid_bjninfo->tagOfBjnFile,"GDCponimaesh",LLABEL);
	str_alloc_cpy(grid_bjninfo->filename,COORDS);
	str_alloc_cpy(grid_bjninfo->gridname,COORDS);

	grid_bjninfo->nBitsReduce=(LPINT)
		alloc_char_mas_n1(grid_bjninfo->n_func*(sizeof(grid_bjninfo->nBitsReduce[0])));

	str_alloc_cpy(grid_bjninfo->funcNames[0],"x");
	str_alloc_cpy(grid_bjninfo->funcNames[1],"y");
	str_alloc_cpy(grid_bjninfo->funcNames[2],"z");

	grid_bjninfo->nBitsReduce[0] = 0;
	grid_bjninfo->nBitsReduce[1] = 0;
	grid_bjninfo->nBitsReduce[2] = 0;

	
	debuf2filePrint(debugFileOutput, "GDC GridInfo ... done\n");
	
	return 0;
}
